<script setup lang="ts">
import { ref } from 'vue'
import type { OperationProps } from '@own-basic-component/ui-naive'
import { BaseTableHelper } from '@own-basic-component/ui-naive'
import { downloadFile, useMessage } from '@own-basic-component/util'
import { columns } from './columns'
import projectGenerateHistoryRequest from '~/api/project-generate-history'
import type { ProjectGenerateHistoryVo } from '~/entity/project/project-generate-history-vo'
import fileRecordRequest from '~/api/file-record'

interface PropsState {
  projectId?: string
  templateId?: string
  title?: string
}

const props = withDefaults(defineProps<PropsState>(), {
  title: '生成历史',
})

const baseTableHelper1 = ref<InstanceType<typeof BaseTableHelper>>()

// 关键字字段
const keywords = ref<string>('')

// 查询前参数获取
function beforeFetch() {
  return Promise.resolve({
    keywords: keywords.value,
  })
}

function refresh() {
  baseTableHelper1.value?.refresh()
}

// 操作列
const operations: OperationProps<ProjectGenerateHistoryVo>[] = [
  {
    title: '下载',
    action: (row: ProjectGenerateHistoryVo) => {
      fileRecordRequest.getById(row.fileId).then((data) => {
        const file = data.data
        useMessage().success('文件包打包完成，即将下载！')
        downloadFile(file.position + file.path, file.name)
      })
    },
  },
]
</script>

<template>
  <NDrawerContent v-once :title="props.title">
    <BaseTableHelper
      ref="baseTableHelper1"
      :columns="columns"
      :operations="operations"
      :operation-extra="{ max: 4 }"
      :fetch-method="projectGenerateHistoryRequest.page"
      :default-params="{ projectId: props.projectId, templateId: props.templateId }"
      :before-fetch="beforeFetch"
      :row-key="row => row.id"
      :pagination="false"
      :bordered="false"
      :default-rows="13"
    >
      <template #search>
        <NInput v-model:value="keywords" placeholder="请输入关键字" @keyup.enter="refresh" />
      </template>
    </BaseTableHelper>
  </NDrawerContent>
</template>

<style scoped lang="scss">

</style>
